package com.ms.oschina.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.Favorite;
import com.ms.oschina.fragment.FavoriteFragment;
import com.ms.oschina.ui.activity.NewsDetailsActivity;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.viewholder.FavoriteViewHolder;

public class FavoriteAdapter extends PageIndexListAdapter<Favorite> {

	public FavoriteAdapter(AbsListView listView, List<Favorite> list,
			boolean loadMore) {
		super(listView, list, loadMore);
	}

	@Override
	public void onNormalItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		Favorite favorite = getListData().get(position);
		Context context = UiUtils.getContext();
		int mark = getMark();
		int detailId = favorite.getId();
		
		Intent intent = new Intent(context, NewsDetailsActivity.class);
		if (mark == FavoriteFragment.Blog) {
			intent.putExtra(Constants.NEWSBLOGDETAILS_ID, detailId);
			intent.putExtra(Constants.MARK, Constants.MARK_NEWSBLOGDETAILS);
		} else if (mark == FavoriteFragment.INFORMATION) {
			intent.putExtra(Constants.NEWSINFODETAILS_ID, detailId);
			intent.putExtra(Constants.MARK, Constants.MARK_NEWSINFODETAILS);
		}

		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	public BaseViewHolder<Favorite> getItemViewHolder() {
		FavoriteViewHolder favoriteViewHolder = new FavoriteViewHolder();
		return favoriteViewHolder;
	}

	@Override
	public List<Favorite> getLoadMoreData() throws Exception {
		int mark = getMark();
		switch (mark) {
		case FavoriteFragment.SOFTWARE:
			return OsChinaApi.getFavoriteInfo(pageIndex,
					Favorite.CATALOG_SOFTWARE);
		case FavoriteFragment.TOPICS:
			return OsChinaApi
					.getFavoriteInfo(pageIndex, Favorite.CATALOG_TOPIC);
		case FavoriteFragment.Code:
			return OsChinaApi.getFavoriteInfo(pageIndex, Favorite.CATALOG_CODE);
		case FavoriteFragment.Blog:
			return OsChinaApi
					.getFavoriteInfo(pageIndex, Favorite.CATALOG_BLOGS);
		case FavoriteFragment.INFORMATION:
			return OsChinaApi.getFavoriteInfo(pageIndex, Favorite.CATALOG_NEWS);
		default:
			return null;
		}
	}

}
